Skip to content

Block Editor: Include resolved bound content when copying blocks - #84428

Open
salvatorecorsi wants to merge 17 commits into
WordPress:trunkfrom
salvatorecorsi:fix/copy-bound-block-content
Open

salvatorecorsi wants to merge 17 commits into
WordPress:trunkfrom
salvatorecorsi:fix/copy-bound-block-content

Conversation

@salvatorecorsi

@salvatorecorsi salvatorecorsi commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #84362. Related to #84389, which guards the selected bound-field RichText copy path.

Include available client-resolved binding values when copying blocks from the canvas, List View and the options menu. Preserve binding definitions and each attribute's saved fallback when its source cannot provide a copyable value.

Why?

A bound paragraph can display content while its stored content is empty, producing an empty copy. A display-only field key or label can also overwrite a saved fallback. Resolving attributes independently matters for blocks such as Button: an available label should survive even when its bound URL is unavailable.

Clipboard transport must retain the block comments carrying bindings, including when an asynchronous write is needed before Cut can remove the source. Copying an individual List Item also needs its parent List for valid paste, while Paste styles needs the selected item's styles.

How?

Client binding sources explicitly opt in with the optional supportsCopy: true registration property. Only an exact true allows the copy resolver to call getValues with isForCopy: true; absent or false preserves saved fallbacks. Registration rejects defined non-boolean values. This leaves existing sources' display behavior intact and lets opted-in sources omit attributes that only have display placeholders.

The four built-in sources, core/post-meta, core/post-data, core/pattern-overrides and core/term-data, opt in. Unavailable term keys/labels and other display placeholders are omitted on copy; genuine values equal to their keys or labels, empty values and readable read-only values remain copyable. The documented date source also opts in: missing or null meta is omitted for copy, a genuine empty value stays empty, and display keeps its existing fallback to today's date.

Eligible mounted block edits register a required, lazy copy getter in the existing per-editor refs context. Copy reads current values and applies only available bound attributes to cloned selected blocks. Each omitted attribute retains its saved fallback, with bindings and source blocks unchanged. Stale, duplicate, preview, Query Loop and Terms Query contexts are excluded. Rendering continues to use the display attributes; the private copy ref no longer stores a display snapshot fallback.

Canvas, List View and menu copies share the payload builder. For resolved bound values, the menu first tries a synchronous copy event for rich HTML and visible plain text; if unavailable, it writes the complete serialized markup as text. Other menu copies retain their serialized-text transport. Cut waits for a successful write before removing the source. Options Copy consistently requests wrappers for both bound and unbound List Items.

A clipboard-only property in the synthetic parent's opening delimiter identifies the wrapper without introducing a preceding Classic block in an unaware parser. Normal paste consumes only that marker and keeps the List; Paste styles uses the selected children. Copying an actual List uses its own styles. Marker insertion/removal preserves the other serialized attribute bytes. Windows fragment and Chromium metadata cleanup applies only to actual text/html, preserving literal fragment comments in serialized plain-text payloads.

Testing Instructions

  1. Bind a paragraph with empty saved content to an available source value such as Bound <strong>text</strong>. Custom client sources must register supportsCopy: true and return available values for isForCopy requests. Copy from the canvas, List View and options menu; paste into Gutenberg and inspect the content, valid serialization and metadata.bindings. Check that the original and its source are unchanged.
  2. Bind both Button label and URL but provide only the label. Verify the copied label and saved URL fallback. Repeat with mixed selections and partial RichText endpoints.
  3. Bind saved fallback content to the inaccessible core/post-meta key unaccessible_field. Its displayed key must not replace the fallback. Repeat with an unavailable term field, an unaware custom source, genuine empty values and genuine values equal to a field key or label.
  4. Check the date example with missing, null, empty and valid date values. Display keeps its current fallback; copied values omit missing/null, preserve empty, and preserve a valid stored date. Check that Query Loop and Terms Query instances do not register copy values.
  5. Make the menu copy event unavailable, then Cut and Paste. The source must remain while the asynchronous write is pending; after success, the pasted block must retain its binding, content and validity.
  6. Copy an individual List Item from the options menu, both bound and unbound, inside an ordered List. Paste should retain a valid ordered List wrapper. Repeat from the canvas and List View where applicable.
  7. Give the List and selected item different styles. Paste styles should use the item's styles when it was copied individually, and the List's styles when the whole List was copied. Parse the serialized payload without marker support: expect one valid List and no preceding Classic block.
  8. Copy styles from a styled Group containing a Custom HTML child with literal <!--StartFragment--> and <!--EndFragment--> comments. Paste styles must preserve the Group's styles when the clipboard transport contains serialized plain text.

Testing Instructions for Keyboard

Use Ctrl+C/Cmd+C and Ctrl+V/Cmd+V with block selections in the canvas and List View. Extend and reverse selections with the keyboard, and repeat with partial RichText endpoints. Focus a List View row, open its options menu with the keyboard, and activate Copy or Cut. Verify the source remains selected and present until a pending Cut write completes.

Historical validation — 24 November 2026

Published head: 57715250e3fc7c8e896ac7a1f40aa6d005c53c78, incorporating trunk f0dbebf61914de11d7f79a0e1558ed46dbfd5c14.

  • The final candidate's focused regression run passed 306 tests across 15 files, covering source opt-in, unavailable and genuine values, mounted copy refs, wrappers, transport and paste behavior.
  • Full production build, full repository typecheck, scoped JavaScript lint, Vitest conventions, tsconfig, all three PR changelogs and generated-documentation checks passed. Scoped lint retains one usePasteStyles registry dependency warning with the same dependency/member-use pattern as F0. Raw Markdown lint still reports the six unchanged MD060 diagnostics at line 126; that is a verified baseline exception, not a Markdown lint pass.
  • The scoped published-dependency audits for blocks, block-editor and editor reported zero findings through the Windows junction adapter. Editor retains four unchecked dynamic-specifier occurrences. These scoped results are separate from the default full audit.
  • Six fresh automated Chromium 158 cases passed in the actual post editor: controlled pending-write Cut, native Paste, inaccessible post-meta copy from the canvas and List View, an unaware source retaining its saved fallback, and an unbound ordered List Item retaining its wrapper. Three served assets matched this built checkout before/after. The owned draft, session, full preferences snapshot, plugin mount and server state were restored.
  • The full upstream staged taskset passed serially on Windows, preserving the exact reviewed tree and ordered merge parents. Hosted CI is tracked separately from these local results; the missing type label still needs a maintainer.

The platform payload and unaware-parser controls are focused tests, not an older-WordPress or browser matrix. Genuine readable meta key/empty controls remain source tests; the native plan adds no readable meta schema or plugin. No Docker E2E, Safari/WebKit, full repository functional suite or human testing pass is claimed.

Screenshots or screencast

Not applicable: this changes clipboard payloads.

Could a maintainer add the [Type] Bug label? I do not have permission to apply upstream labels. The optional supportsCopy registration property and isForCopy callback argument need API review; if accepted, could a maintainer add Needs Dev Note?

Branch update — 24 November 2026 (current trunk 6b92844)

Merged upstream trunk 6b928443b3df5480924958df2dcae0dae692dadb to resolve the actual package-changelog conflict. The original source and regression-test contribution is retained. Upstream release sections are preserved, with this PR's notes only in the current Unreleased categories, after upstream notes.

Fresh validation on published head 794e519a0d54103f9a99f2a7e854af092168065e: verified reuse of this checkout’s isolated dependency installation, production build, PR changelog validation, full repository typecheck, the scoped lint/static-analysis checks, test conventions, tsconfig validation, generated-documentation checks passed. 308 focused test executions passed across 16 test-file executions. The documentation lint comparison reproduced only the same six MD060 diagnostics at the unchanged table row; this is a baseline comparison, not a Markdown lint pass. The three scoped published-dependency audits use the Windows junction adapter and remain separate from the default full audit; unchecked dynamic specifiers remain recorded in their result files.

These are scoped checks, not a full repository functional-suite or Docker E2E pass. Fresh Chromium post-editor verification passed for the canvas, List View and menu clipboard paths. The owned draft, preferences, session, mount and server state were restored. Hosted CI and maintainer-only metadata checks are separate from these local results.

Use of AI Tools

OpenAI Codex assisted with investigation, implementation, regression tests, automated source review and validation, including the focused native-editor Chromium adapter. Maintainer review is requested.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @salvatorecorsi! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: salvatorecorsi <salvatorecorsi@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 9, 2026
@github-actions
github-actions Bot requested review from aduth and ciampo November 16, 2026 10:39

@ciampo ciampo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the full context on the block bindings work, but I took a look from the menu and clipboard side and left a few comments inline. Please don't count me as the main reviewer for this one.

: getClipboardBlocksContent(
getBlocksByClientId( clientIds ) as Block[],
registry,
attributesForCopy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying a bound List Item from an ordered list produces a bare <li> here. Could the rich menu copy honor requiresWrapperOnCopy so it retains the parent <ol> the same way keyboard copy does?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordered list now pastes with its wrapper. I found one side effect for Paste styles. With a large bound List Item inside a small List, copying the item applies small to the target. Could we keep the wrapper for regular paste and use the selected item's styles for Paste styles?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paste styles now reads the selected item’s styles while normal paste keeps the ordered-list wrapper. A clipboard-only marker distinguishes a synthetic wrapper from an actually selected List and is removed before insertion, so copying the List itself still uses its own styles.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selected item’s styles now paste correctly. Pasting the new markup into an older editor adds a Classic block before the List. Could we put the marker inside the List’s opening block delimiter so older editors can ignore it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! The marker is now an extra attribute inside the synthetic List's opening block delimiter. An unaware parser can ignore it, and Paste styles can still distinguish that wrapper from a List the user selected. The regression parses the raw copied HTML without marker removal and gets one valid List, with no preceding Classic block.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The marker inside the delimiter works with older editors now. The menu still adds the wrapper only when a bound value resolves, so the same List Item copies with or without its List depending on whether the data has loaded. Could Options → Copy use addWrapper: true like keyboard copy? Then the 'bound' mode could go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options → Copy now always uses addWrapper: true, and the 'bound' mode is gone. A List Item keeps the same parent wrapper while its data loads; Paste styles still reads the item's styles.

@salvatorecorsi

Copy link
Copy Markdown
Contributor Author

Thanks for checking these clipboard paths. The update in 53cf427a tries the synchronous copy event first so the block comments can travel with the visible text. Paste styles reads the serialized HTML when available and still accepts the existing serialized text format. Menu copy keeps the ordered-list wrapper for a bound item, and unbound Copy and Cut reuse the same serialization.

I verified the menu copy, pasting back with the binding, and Paste styles in the actual post editor in Chromium. The menu round trip is also in the WebKit Playwright project. My Windows WebKit 27.2 build did not return the rich clipboard payload even in a minimal copy-event control, so the Safari round trip still needs confirmation.

ciampo
ciampo previously requested changes Nov 21, 2026
.filter(
( [ attributeName, binding ] ) =>
bindableAttributes?.includes( attributeName ) &&
registeredSources[ binding?.source as string ]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With testing/server-only-source, the paragraph stores "paragraph default content" and the editor displays "Server Source". Copying the block now makes "Server Source" the pasted paragraph's saved content. Could we preserve the original fallback when the editor only has a placeholder? The existing server-only fixture would cover this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :D Server-only sources now stay out of the editor snapshot, because their displayed text is a placeholder rather than a value we can save. Copy retains the paragraph’s saved fallback; the regression covers a source without getValues.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server-only case is fixed. With the existing inaccessible core/post-meta field, copying still changes the paragraph’s saved text from fallback content to unaccessible_field. Could we keep the saved text when the source returns a field key for display?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — that key is a display placeholder, so it mustn't replace the saved fallback. Copy now resolves values with an optional isForCopy request, letting the source omit unavailable values while retaining genuine key/label matches and empty values. I verified Ctrl+C for your inaccessible post-meta case in the real post editor: the fallback and binding survive, and the source block stays unchanged. The callback addition is documented for API review.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The post-meta case keeps its saved text now, thanks. core/term-data still returns its field label when the term isn't available. Copying a Navigation Link then saves "url":null, and a bound paragraph saves Name. Could term-data skip those values when isForCopy is set, the same way post-data does?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :D core/term-data now omits unavailable fields for copy, so the saved fallback survives instead of a display key or label. Genuine values, including empty values and values equal to a label, are still copied.

: getClipboardBlocksContent(
getBlocksByClientId( clientIds ) as Block[],
registry,
attributesForCopy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordered list now pastes with its wrapper. I found one side effect for Paste styles. With a large bound List Item inside a small List, copying the item applies small to the target. Could we keep the wrapper for regular paste and use the selected item's styles for Paste styles?

@ciampo
ciampo dismissed their stale review November 21, 2026 00:29

The inline feedback is advisory.

@github-actions github-actions Bot added [Package] Blocks /packages/blocks [Package] Editor /packages/editor labels Nov 24, 2026

@ciampo ciampo left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some more feednack (I don't think any of these are blockinh), more like consistency fixes for the copy paths.

One optional simplification: edit.tsx always provides getAttributesForCopy, so it could be required. Then the computedAttributes fallback in getBoundAttributesForCopy and that dependency of the registration effect could go.

Comment on lines +212 to +213
blockContext.query !== undefined ||
blockContext.queryId !== undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying a Terms Query saves the active row's term name into the template's paragraph fallback, so the copy depends on which row was active. Could we skip termQuery here too, next to query and queryId?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added termQuery alongside query and queryId, including the effect dependencies, so a Terms Query row can't replace the template's saved fallback. The mounted BlockEdit test checks that no copy getter is registered in that context.

The function must return an `object` with this structure:
`{ 'block attribute' : value }`

For copy requests, return the actual source values. Omit an attribute, or return `undefined` for it, when its value is unavailable and the editor displays a placeholder instead. This preserves the attribute's saved fallback in the copied block. Return genuine values even when they are empty, read-only, or equal to a field's key or label.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wpmovies/visualization-date example above still returns today's date when the meta value is missing, so copying the block saves that date as its content. Could the example skip the value when isForCopy is set?

I'm also wondering if existing sources that don't know about isForCopy should opt in to copying resolved values. Without an opt-in, they'll start saving their placeholders.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The date example now omits missing/null metadata on copy and keeps an actual empty string empty; its display still falls back to today's date. Sources must opt in with supportsCopy: true before they receive isForCopy requests, so existing sources keep their saved fallbacks. I also made the private getter required and removed the cached computedAttributes fallback and effect dependency.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block editor /packages/block-editor [Package] Blocks /packages/blocks [Package] Editor /packages/editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants